package com.zhuzhoujia.city_wide_action;

import com.example.zhuzhoujia.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zhuzhoujia.utils.Utils;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

public class AllActivity extends FragmentActivity {
	@ViewInject(R.id.vp)
	private ViewPager vp;
	@ViewInject(R.id.line)
	private TextView line;
	@ViewInject(R.id.all)
	private TextView all;
	@ViewInject(R.id.begin)
	private TextView begin;
	@ViewInject(R.id.over)
	private TextView over;

	private int curX = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Utils utils = new Utils();
		utils.setStatusBarTintResource(this, R.color.green);
		View v = LayoutInflater.from(this).inflate(R.layout.activity_actionbar, null);
		utils.initActionBarByLayout(getActionBar(), this, v, R.color.green);
		setContentView(R.layout.all_activity);
		ViewUtils.inject(this);
		OnPageChange oc = new OnPageChange();
		oc.initViewPagerTitle(0);
		vp.setAdapter(new AllActivityAdapter(getSupportFragmentManager()));
		vp.setOnPageChangeListener(oc);
	}

	class OnPageChange implements OnPageChangeListener {
		private int width;

		public OnPageChange() {
			DisplayMetrics dm = new DisplayMetrics();
			AllActivity.this.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
			width = dm.widthPixels / 3;
			line.getLayoutParams().width = width;
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageSelected(int arg0) {
			initViewPagerTitle(arg0);
			int toX = width * arg0;
			TranslateAnimation animation = new TranslateAnimation(curX, toX, 0, 0);
			animation.setDuration(200);
			animation.setFillAfter(true);
			animation.setInterpolator(new AccelerateDecelerateInterpolator());
			line.startAnimation(animation);
			curX = toX;
		}

		public void initViewPagerTitle(int curPager) {
			TextView[] tvs = { all, begin, over };
			for (int i = 0; i < tvs.length; i++) {
				tvs[i].setTextColor(Color.parseColor("#A9A9A9"));
			}
			tvs[curPager].setTextColor(Color.WHITE);
		}
	}
}
